package com.example.theme_hospital;

import java.awt.*;
import java.util.Random;

public class Patient {
    public int x, y;
    private int speed = 1;
    private int targetX, targetY;
    private boolean reachedTarget = false;
    private Diagnosis diagnosis;
    private boolean isInHospital = false;
    
    public Patient(int x, int y) {
        this.x = x;
        this.y = y;
        // 设置初始目标为地图中心附近
        this.targetX = 10;
        this.targetY = 10;
        
        // 随机生成一个诊断
        initDiagnosis();
    }
    
    private void initDiagnosis() {
        String[] diagnoses = {"普通感冒", "骨折", "胃痛", "头痛", "发烧"};
        Random rand = new Random();
        String name = diagnoses[rand.nextInt(diagnoses.length)];
        int severity = rand.nextInt(5) + 1; // 严重程度1-5
        int treatmentTime = severity * 100; // 治疗时间与严重程度相关
        
        this.diagnosis = new Diagnosis(name, severity, treatmentTime);
    }
    
    public void move(int[][] map, int mapWidth, int mapHeight) {
        // 简单的寻路逻辑：直接朝目标点移动
        if (!reachedTarget) {
            int dx = 0, dy = 0;
            
            if (x < targetX) dx = 1;
            else if (x > targetX) dx = -1;
            
            if (y < targetY) dy = 1;
            else if (y > targetY) dy = -1;
            
            // 计算新位置
            int newX = x + dx * speed;
            int newY = y + dy * speed;
            
            // 检查边界和碰撞
            if (newX >= 0 && newX < mapWidth && newY >= 0 && newY < mapHeight) {
                // 检查是否可以移动到新位置（不是墙壁）
                if (map[newY][newX] != HospitalGamePanel.WALL) {
                    x = newX;
                    y = newY;
                }
            }
            
            // 检查是否到达目标
            if (x == targetX && y == targetY) {
                reachedTarget = true;
                // 如果目标是医院并且还未进入医院，则设置新的目标进入医院
                if (!isInHospital && targetX == 10 && targetY == 5) {
                    // 进入医院内部
                    targetX = 10;
                    targetY = 5;
                    isInHospital = true;
                }
            }
        }
    }
    
    public void update(int[][] map, int mapWidth, int mapHeight) {
        // 更新病人状态
        move(map, mapWidth, mapHeight);
    }
    
    public void render(Graphics2D g, int isoX, int isoY) {
        // 绘制病人为黄色菱形
        g.setColor(Color.YELLOW);
        
        // 绘制菱形
        int[] xPoints = {isoX, isoX + HospitalGamePanel.TILE_WIDTH/4, isoX, isoX - HospitalGamePanel.TILE_WIDTH/4};
        int[] yPoints = {isoY - HospitalGamePanel.TILE_HEIGHT/4, isoY, isoY + HospitalGamePanel.TILE_HEIGHT/4, isoY};
        g.fillPolygon(xPoints, yPoints, 4);
        
        // 绘制网格线
        g.setColor(Color.BLACK);
        g.drawPolygon(xPoints, yPoints, 4);
        
        // 如果已诊断，显示诊断信息
        if (diagnosis != null && diagnosis.isDiagnosed()) {
            g.setColor(Color.WHITE); // 使用与地面反差较大的颜色
            g.setFont(new Font("Arial", Font.BOLD, 10)); // 使用粗体字体
            String severityStr = "Lv" + diagnosis.getSeverity();
            FontMetrics fm = g.getFontMetrics();
            int textWidth = fm.stringWidth(severityStr);
            g.drawString(severityStr, isoX - textWidth/2, isoY - HospitalGamePanel.TILE_HEIGHT/4 - 5);
        }
    }
    
    // 设置新的目标点
    public void setTarget(int targetX, int targetY) {
        this.targetX = targetX;
        this.targetY = targetY;
        this.reachedTarget = false;
    }
    
    // Getters
    public Diagnosis getDiagnosis() {
        return diagnosis;
    }
    
    public boolean isInHospital() {
        return isInHospital;
    }
}